import ExamStatus from '@/components/type-of-exam/types/exam-status';
import { DetailsQo } from '@/services/quest';
import { DetailResult } from '@/store/answer/types';
// import { DetailResult } from '@/store/answer/types';
// import { DetailResult } from '@/store/answer/types';
import { ExamItem } from '../types';
import BankAdapter from './exam-type-adapter/BankAdapter';
import ChosesListAdapter from './exam-type-adapter/ChosesListAdapter';

export default class DetailItemAdapter {
  parse(value: ExamItem): DetailsQo {
    const qo: DetailsQo = {};

    qo.title = value.title;

    qo.typeId = String(value.typeId);

    qo.required = Number(value.require);

    if (value.options) {
      qo.options = new ChosesListAdapter().parse(value.options);
    }

    return qo;
  }

  compose(value: DetailResult, index: number, status: ExamStatus): ExamItem {
    const vo: ExamItem = {
      detailsId: value.detailsId,
      queId: value.queId,
      require: Boolean(value.required),
      title: value.title,
      typeId: Number(value.typeId),
      typeName: value.typeName,
      edit: false,
      questNum: index + 1,
      status: status,
      answerChose: '',
    };

    if (value.options) {
      vo.options = new ChosesListAdapter().compose(value.options);
    }

    if (value.chooseInfos && value.chooseInfos.length) {
      // const count: number = value.chooseInfos.reduce(
      //   (a, b) => a.answerPercent + b.answerPercent,
      // );
      let sum = 0;
      for (const item of value.chooseInfos) {
        sum += item.answerPercent;
      }
      vo.chooseInfos = new ChosesListAdapter().compose(value.chooseInfos, sum);
    }

    if (value.blankInfos && value.blankInfos.length) {
      vo.blankInfos = new BankAdapter().compose(value.blankInfos);
    }

    return vo;
  }
}
